/*
import {getRequest} from './api'

//初始化菜单数据
export const initMenu = (router,store)=>{
    //如果store里的routes为空，就返回一个空
    if(store.state.routes.length>0){
        return null
    }
    //调用接口
    getRequest('/system/cfg/menu').then(data=>{
        if(data){
            //初始化路由
            let fmtRoutes = formatRoutes(data)
            //添加到路由
            router.addRoutes(fmtRoutes)
            //将数据传入vuex
            store.commit('initRoutes',fmtRoutes)
        }
    })

}
export const formatRoutes = (routes)=>{
   let fmtRoutes = []
    routes.forEach(router=>{
        let {
            path,
            component,
            name,
            children,
            iconsCls
        }= router
        if (children && children instanceof Array){
            //如果路由里有children和children是个数组
            //递归
            children = formatRoutes(children)
        }
        let fmtRouter = {
            path:path,
            name:name,
            iconsCls:iconsCls,
            children:children,
            component(resolve){ //主动去加载组件
                require(['../views'+component + '.vue'],resolve)
            }
        }
        fmtRoutes.push(fmtRouter)
    })
    return fmtRoutes
}*/



/*import {getRequest} from './api'

export const initMenu=(store,router)=>{
    //判断路由里routes有没有值
    if(store.state.routes.length>0){
        return null
    }
    getRequest('/system/cfg/menu').then(data=>{
        //初始化路由
        let fmtRoutes = formatRoutes(data)
        //添加路由
        router.addRoutes(fmtRoutes)
        //添加到vuex
        store.commit('initRoutes',fmtRoutes)

    })
}
export const formatRoutes=(routes)=>{
    let fmtRoutes = []
    routes.forEach(router => {
        let {
            path,
            component,
            name,
            iconCls,
            children,
        } = router;
        if(children&&children instanceof Array){
            children = formatRoutes(children)
        }
        let fmtRouter={
            path:path,
            name:name,
            iconCls:iconCls,
            children:children,
            component(resolve){
                if(component.startWith('Emp')){
                    require(['../views/emp/'+component + '.vue'],resolve)
                }else if(component.startWith('Per')){
                    require(['../views/per/'+component + '.vue'],resolve)
                }else if(component.startWith('Sal')){
                    require(['../views/sal/'+component + '.vue'],resolve)
                }else if(component.startWith('Sta')){
                    require(['../views/sta/'+component + '.vue'],resolve)
                }else if(component.startWith('Sys')){
                    require(['../views/sys/'+component + '.vue'],resolve)
                }

            }
        }
        fmtRoutes.push(fmtRouter)
    })
    return fmtRoutes
}*/




import {getRequest} from "./api";

export const initMenu = (router, store) => {
    if (store.state.routes.length > 0) {
        return
    }
    getRequest('/system/cfg/menu').then(data => {
        if (data) {
            // 格式化Router
            let fmtRoutes = formatRoutes(data);
            // 添加到router
            router.addRoutes(fmtRoutes);
            // 将数据存入vuex
            store.commit('initRoutes', fmtRoutes);
        }
    })
}


export const formatRoutes = (routes) => {
    let fmtRoutes = [];
    routes.forEach(router => {
        let {
            path,
            component,
            name,
            iconCls,
            children,
        } = router;
        if (children && children instanceof Array) {
            // 递归
            children = formatRoutes(children);
        }
        let fmRouter = {
            path: path,
            name: name,
            iconCls: iconCls,
            children: children,
            component(resolve) {
                if (component.startsWith('Home')) {
                    require(['../views/' + component + '.vue'], resolve);
                }
                   else if (component.startsWith('Emp')) {
                    require(['../views/emp/' + component + '.vue'], resolve);
                } else if (component.startsWith('Per')) {
                    require(['../views/per/' + component + '.vue'], resolve);
                } else if (component.startsWith('Sal')) {
                    require(['../views/sal/' + component + '.vue'], resolve);
                } else if (component.startsWith('Sta')) {
                    require(['../views/sta/' + component + '.vue'], resolve);
                } else if (component.startsWith('Sys')) {
                    require(['../views/sys/' + component + '.vue'], resolve);
                }
            }
        }
        fmtRoutes.push(fmRouter)
    })
    return fmtRoutes;
}
















